import { Navigate } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { RootState } from '@/redux/store';
import { useEffect } from 'react';

interface PrivateRouteProps {
  children: React.ReactNode;
}

const PrivateRoute: React.FC<PrivateRouteProps> = ({ children }) => {
  const { isAuthenticated, token } = useSelector((state: RootState) => state.auth);
  
  useEffect(() => {
    console.log('PrivateRoute 认证状态:', { 
      isAuthenticated, 
      token,
      localStorageToken: localStorage.getItem('token')
    });
  }, [isAuthenticated, token]);

  if (!isAuthenticated) {
    console.log('未认证，重定向到登录页');
    return <Navigate to="/login" replace />;
  }

  console.log('已认证，显示受保护内容');
  return <>{children}</>;
};

export default PrivateRoute; 